package org.ative.chs;

import java.util.NoSuchElementException;

public class UciStringTokenizer {
 
	private String str;    
    private int currentPosition;  
      
    public UciStringTokenizer(String str) {  
        this.str = str.trim();  
    }  
   
    /**
     * <b>nextToken</b><p/>
	 * <code>public String <b>nextToken</b>()</code><p/>
	 * Returns the next token from this UCI string tokenizer. 
     * @return	the next token from this UCI string tokenizer.
     * @throws 	NoSuchElementException
     */
    public String nextToken() throws NoSuchElementException {  
    	//move through any existing whitespace at currentPosition
    	while (str.substring(currentPosition, currentPosition + 1).equals(" ")){
    		currentPosition++;
    	}
        int nextWhiteSpace = str.length();
        String ch = "";
        for (int i = currentPosition; i < str.length(); i++) { 
        	ch = str.substring(i, i+1);
        	if (ch.equals(" ")) {
        		nextWhiteSpace = i;
        		break;
        	}
        }  
        String token = str.substring(currentPosition, nextWhiteSpace);  
        currentPosition = nextWhiteSpace + 1;  
        return token;  
    }  
   
    /**
     * <b>hasMoreTokens</b><p/>
	 * <code>public boolean <b>hasMoreTokens</b>()</code><p/>
	 * <p>Tests if there are more tokens available from this UCI tokenizer's 
	 * string. If this method returns true, then a subsequent call to nextToken 
	 * with no argument will successfully return a token.</p>
	 * <p>This can be a new game the engine should play or a new game it 
	 * should analyze but also the next position from a testsuite with 
	 * positions only.</p>
     * @return	true if and only if there is at least one token in the string 
     * after the current position; false otherwise.
     */
    public boolean hasMoreTokens() {  
    	if (currentPosition < str.length())
    		return true;
    	else
    		return false;
    } 
}
